<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>ES6 新增用法 ( 六 ) | Saturday_</title><meta name="keywords" content="js,javascript,ES6,class,export,import,module,extends"><meta name="author" content="HHR"><meta name="copyright" content="HHR"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="类基本用法类的属性和方法声明一个类的写法 : 1234567&#x2F;&#x2F; 定义一个叫  Animal 的类class Animal &amp;#123;  &#x2F;&#x2F; constructor 构造函数  constructor(color) &amp;#123;    this.color &#x3D; color;  &amp;#125;&amp;#125;  可以看到类里面 ( 花括号 &amp;#123;&amp;#125; 里面 ) 有一个叫 construc">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6 新增用法 ( 六 )">
<meta property="og:url" content="https://codehhr.cn/2021/06/25/js/es6-day06/index.html">
<meta property="og:site_name" content="Saturday_">
<meta property="og:description" content="类基本用法类的属性和方法声明一个类的写法 : 1234567&#x2F;&#x2F; 定义一个叫  Animal 的类class Animal &amp;#123;  &#x2F;&#x2F; constructor 构造函数  constructor(color) &amp;#123;    this.color &#x3D; color;  &amp;#125;&amp;#125;  可以看到类里面 ( 花括号 &amp;#123;&amp;#125; 里面 ) 有一个叫 construc">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png">
<meta property="article:published_time" content="2021-06-25T11:17:42.000Z">
<meta property="article:modified_time" content="2021-06-28T10:45:47.006Z">
<meta property="article:author" content="HHR">
<meta property="article:tag" content="js">
<meta property="article:tag" content="javascript">
<meta property="article:tag" content="ES6">
<meta property="article:tag" content="class">
<meta property="article:tag" content="export">
<meta property="article:tag" content="import">
<meta property="article:tag" content="module">
<meta property="article:tag" content="extends">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="https://codehhr.cn/2021/06/25/js/es6-day06/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: {"limitDay":680,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: HHR","link":"链接: ","source":"来源: Saturday_","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'ES6 新增用法 ( 六 )',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-06-28 18:45:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="/js/jquery.min.js"></script><link rel="stylesheet" href="/css/cursor.css"><link rel="stylesheet" href="/css/footergradient.css"><link rel="stylesheet" href="/css/scrollbar.css"><link rel="stylesheet" href="/css/animate.min.css"><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Saturday_" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">87</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">120</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-image"></i><span> 照片</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/school/"><i class="fa-fw fas fa-graduation-cap"></i><span> 科大</span></a></li><li><a class="site-page child" href="/wallpaper/"><i class="fa-fw fas fa-image"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/csslayouts/sunrise.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Saturday_</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-image"></i><span> 照片</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/school/"><i class="fa-fw fas fa-graduation-cap"></i><span> 科大</span></a></li><li><a class="site-page child" href="/wallpaper/"><i class="fa-fw fas fa-image"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">ES6 新增用法 ( 六 )</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-06-25T11:17:42.000Z" title="发表于 2021-06-25 19:17:42">2021-06-25</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-06-28T10:45:47.006Z" title="更新于 2021-06-28 18:45:47">2021-06-28</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/js/">js</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/js/ES6/">ES6</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="ES6 新增用法 ( 六 )"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="类基本用法"><a href="#类基本用法" class="headerlink" title="类基本用法"></a>类基本用法</h1><h2 id="类的属性和方法"><a href="#类的属性和方法" class="headerlink" title="类的属性和方法"></a>类的属性和方法</h2><p>声明一个类的写法 :</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个叫  Animal 的类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="comment">// constructor 构造函数</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">color</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.color = color;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到类里面 ( 花括号 <code>&#123;&#125;</code> 里面 ) 有一个叫 <code>constructor</code> 方法 , 它就是构造方法 , 构造方法里面的 <code>this</code> , 指向的是该类实例化后的对象 , 这就是实现了一个类的声明 , 如果你没有编写 <code>constructor</code> 方法 , 执行的时候也会被加上一个默认的空的 <code>constructor</code> 方法 , constructor 方法是必须的 , 也是唯一的 , 一个类体不能含有多个 constructor 构造方法</p>
<h2 id="类的实例化对象"><a href="#类的实例化对象" class="headerlink" title="类的实例化对象"></a>类的实例化对象</h2><p>给类添加属性和方法 , 把类名后面的括号 <code>&#123;&#125;</code> 里面的内容称之为类体 , 我们会在类体内来编写类的属性和方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 构造方法</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name</span>)</span> &#123;</span><br><span class="line">    <span class="comment">// 属性</span></span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 自定义方法 getName()</span></span><br><span class="line">  <span class="function"><span class="title">getName</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.name;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="类的自定义方法"><a href="#类的自定义方法" class="headerlink" title="类的自定义方法"></a>类的自定义方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 构造方法</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name</span>)</span> &#123;</span><br><span class="line">    <span class="comment">// 属性</span></span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 自定义方法 getName()</span></span><br><span class="line">  <span class="function"><span class="title">getName</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`我想念 <span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span>`</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建一个 Animal 实例对象 dog</span></span><br><span class="line"><span class="keyword">let</span> dog = <span class="keyword">new</span> Animal(<span class="string">&quot;村口消失的大黄&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(dog.name); <span class="comment">// 村口消失的大黄</span></span><br><span class="line"><span class="built_in">console</span>.log(dog.getName()); <span class="comment">// 我想念 村口消失的大黄</span></span><br></pre></td></tr></table></figure>

<h2 id="类的静态方法"><a href="#类的静态方法" class="headerlink" title="类的静态方法"></a>类的静态方法</h2><p>如果在一个方法前 , 加上 <code>static</code> 关键字 , 就表示该方法<strong>不会被实例继承</strong> , 而是直接通过类来调用 , 这就称为 “静态方法” , 类的静态方法可以被类直接调用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Father</span> </span>&#123;</span><br><span class="line">  <span class="keyword">static</span> <span class="function"><span class="title">hello</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(Father.hello());</span><br></pre></td></tr></table></figure>

<h2 id="类的继承"><a href="#类的继承" class="headerlink" title="类的继承"></a>类的继承</h2><p><code>ES6</code> 使用 <code>extends</code> 关键字来实现子类继承父类<br><code>super</code> , 它相当于是父类中的 <code>this</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 父类 Animal</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 子类 Dog</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 构造方法</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name, color</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(name);</span><br><span class="line">    <span class="built_in">this</span>.color = color;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>可以用 <code>super</code> 来引用父类 , 访问父类的方法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 构造方法</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name</span>)</span> &#123;</span><br><span class="line">    <span class="comment">// 属性</span></span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 父类自定义方法</span></span><br><span class="line">  <span class="function"><span class="title">hello</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;Hello ~&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 子类 Dog</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 构造方法</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name, color</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(name);</span><br><span class="line">    <span class="built_in">this</span>.color = color;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 子类的实例方法</span></span><br><span class="line">  <span class="function"><span class="title">getSomething</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">super</span>.hello();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 创建 dog 实例对象</span></span><br><span class="line"><span class="keyword">let</span> dog = <span class="keyword">new</span> Dog(<span class="string">&quot;dog&quot;</span>, <span class="string">&quot;orange&quot;</span>);</span><br><span class="line"><span class="comment">// 调用子类方法</span></span><br><span class="line"><span class="built_in">console</span>.log(dog.getSomething());</span><br></pre></td></tr></table></figure>

<p>在父类中 , 我们定义了 <code>say</code> 方法 , 想要在子类中调用父类的 <code>say</code> 方法的话 , 我们使用 <code>super.say()</code> 即可实现</p>
<p><strong>使用 <code>super</code> 有几个要注意的事项 :</strong></p>
<ol>
<li>子类必须在 <code>constructor</code> 方法中调用 <code>super</code> 方法</li>
<li>调用 <code>super()</code> , 才可以使用 <code>this</code> , 否则报错</li>
</ol>
<h1 id="Module-模块"><a href="#Module-模块" class="headerlink" title="Module 模块"></a>Module 模块</h1><h2 id="模块化的初衷"><a href="#模块化的初衷" class="headerlink" title="模块化的初衷"></a>模块化的初衷</h2><p>高内聚,低耦合</p>
<p>目前还没有浏览器支持 <code>ES6</code> 的 <code>module</code> 模块<br>但是可以解决 :</p>
<ol>
<li>vscode 安装 live sever</li>
<li><code>&lt;script type=&quot;module&quot;&gt;</code> 将 <code>script</code> 标签中的 <code>type</code> 的类型换成 <code>module</code> , 告诉浏览器我们要使用 <code>es6</code> 的模块化了</li>
</ol>
<h2 id="导出-Export"><a href="#导出-Export" class="headerlink" title="导出 Export"></a>导出 Export</h2><p>导出 <code>Export</code> : 作为一个模块 , 它可以选择性地给其他模块暴露 ( 提供 ) 自己的属性和方法 , 供其他模块使用</p>
<h2 id="导入-Import"><a href="#导入-Import" class="headerlink" title="导入 Import"></a>导入 Import</h2><p>导入 <code>Import</code> : 作为一个模块 , 可以根据需要 , 引入其他模块的提供的属性或者方法 , 供自己模块使用</p>
<h2 id="实现-导入-Import-和-导出-Export"><a href="#实现-导入-Import-和-导出-Export" class="headerlink" title="实现 导入 Import 和 导出 Export"></a>实现 导入 Import 和 导出 Export</h2><p>假设 <code>moduleB</code> 模块代码 :</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="comment">// 导出变量 name</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;前端君&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; name &#125;;</span><br></pre></td></tr></table></figure>

<p>假设 <code>moduleA</code> 模块代码 :</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="comment">// 导入 moduleB 的属性 name</span></span><br><span class="line"><span class="keyword">import</span> &#123; name &#125; <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(name); <span class="comment">// 前端君</span></span><br></pre></td></tr></table></figure>

<h2 id="批量-导入-和-导出"><a href="#批量-导入-和-导出" class="headerlink" title="批量 导入 和 导出"></a>批量 导入 和 导出</h2><p>模块 B 批量导出</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;前端君&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> age = <span class="number">20</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hello</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;Hello ~ 前端君&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> &#123; name, age, hello &#125;;</span><br></pre></td></tr></table></figure>

<p>模块 A 批量导入</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; name, age, hello &#125; <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(name); <span class="comment">// 前端君</span></span><br><span class="line"><span class="built_in">console</span>.log(age); <span class="comment">// 20</span></span><br><span class="line">hello(); <span class="comment">// Hello ~ 前端君</span></span><br></pre></td></tr></table></figure>

<h2 id="重命名导出的变量"><a href="#重命名导出的变量" class="headerlink" title="重命名导出的变量"></a>重命名导出的变量</h2><p>想给导入的变量换一个名字的话 , 可以这样做 :<br>使用关键字 <code>as</code> , 可以实现给变量重命名</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; name <span class="keyword">as</span> newName &#125; <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(newName); <span class="comment">// 前端君</span></span><br></pre></td></tr></table></figure>

<h2 id="整体导入"><a href="#整体导入" class="headerlink" title="整体导入"></a>整体导入</h2><p>可以使用星号 <code>*</code> 实现整体导入 :<br>使用星号符 <code>*</code> 将模块 <code>B</code> 提供的所有属性和方法整体导入赋值给变量 <code>obj</code> , 我们可以用点运算符 <code>.</code> 来获取它的属性和方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;前端君&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> age = <span class="number">20</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hello</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;Hello ~ 前端君&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> &#123; name, age, hello &#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> obj <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(obj.name); <span class="comment">// 前端君</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.age); <span class="comment">// 20</span></span><br><span class="line">obj.hello(); <span class="comment">// Hello ~ 前端君</span></span><br></pre></td></tr></table></figure>

<h2 id="默认导出"><a href="#默认导出" class="headerlink" title="默认导出"></a>默认导出</h2><p>默认导出 , 每个模块支持导出一个没有名字的变量 , 使用关键语句 <code>export default</code> 来实现 :</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;I&#x27;m a default function.&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>导入这个模块的时候 , 可以为这个匿名函数取任意的名字 , 我们试一下导入上面那个匿名函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> defaultFunction <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line">defaultFunction(); <span class="comment">// I&#x27;m a default function.</span></span><br></pre></td></tr></table></figure>

<h2 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h2><h3 id="声明的变量-对外都是只读的"><a href="#声明的变量-对外都是只读的" class="headerlink" title="声明的变量 , 对外都是只读的"></a>声明的变量 , 对外都是只读的</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;前端君&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; name &#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; name &#125; <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line">name = <span class="string">&quot;tom&quot;</span>; <span class="comment">// 报错: name is read-only</span></span><br></pre></td></tr></table></figure>

<p>但是 , 如果模块 <code>B</code> 导出的是对象类型的值 , 就可修改 , 实质上是地址不可修改 , 可修改的是地址指向的内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;张三&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">40</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> &#123; person &#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; person &#125; <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line">person.name = <span class="string">&quot;李四&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(person.name); <span class="comment">// 李四</span></span><br></pre></td></tr></table></figure>

<h3 id="导入不存在的变量会报错"><a href="#导入不存在的变量会报错" class="headerlink" title="导入不存在的变量会报错"></a>导入不存在的变量会报错</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleB.js</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;前端君&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> &#123; name &#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// moduleA.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; height &#125; <span class="keyword">from</span> <span class="string">&quot;./moduleB.js&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(height); <span class="comment">// 报错: does not provide an export named &#x27;height&#x27;</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">HHR</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://codehhr.cn/2021/06/25/js/es6-day06/">https://codehhr.cn/2021/06/25/js/es6-day06/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://codehhr.cn" target="_blank">Saturday_</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/js/">js</a><a class="post-meta__tags" href="/tags/javascript/">javascript</a><a class="post-meta__tags" href="/tags/ES6/">ES6</a><a class="post-meta__tags" href="/tags/class/">class</a><a class="post-meta__tags" href="/tags/export/">export</a><a class="post-meta__tags" href="/tags/import/">import</a><a class="post-meta__tags" href="/tags/module/">module</a><a class="post-meta__tags" href="/tags/extends/">extends</a></div><div class="post_share"><div class="social-share" data-image="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/06/25/js/es7-async-await/"><img class="prev-cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">ES7 中的 Async/await</div></div></a></div><div class="next-post pull-right"><a href="/2021/06/25/js/es6-day05/"><img class="next-cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">ES6 新增用法 ( 五 )</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/06/19/js/es6-day01/" title="ES6 新增用法 ( 一 )"><img class="cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-06-29</div><div class="title">ES6 新增用法 ( 一 )</div></div></a></div><div><a href="/2021/06/20/js/es6-day02/" title="ES6 新增用法 ( 二 )"><img class="cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-06-25</div><div class="title">ES6 新增用法 ( 二 )</div></div></a></div><div><a href="/2021/06/23/js/es6-day03/" title="ES6 新增用法 ( 三 )"><img class="cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-06-24</div><div class="title">ES6 新增用法 ( 三 )</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="avatar"/></div><div class="author-info__name">HHR</div><div class="author-info__description">Linux is great ~</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">87</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">120</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/codehhr"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/codehhr" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1871973389@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">评论关了, 图床也挂了</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%B1%BB%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">类基本用法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.</span> <span class="toc-text">类的属性和方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E5%AE%9E%E4%BE%8B%E5%8C%96%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.2.</span> <span class="toc-text">类的实例化对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95"><span class="toc-number">1.3.</span> <span class="toc-text">类的自定义方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="toc-number">1.4.</span> <span class="toc-text">类的静态方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E7%BB%A7%E6%89%BF"><span class="toc-number">1.5.</span> <span class="toc-text">类的继承</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Module-%E6%A8%A1%E5%9D%97"><span class="toc-number">2.</span> <span class="toc-text">Module 模块</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A8%A1%E5%9D%97%E5%8C%96%E7%9A%84%E5%88%9D%E8%A1%B7"><span class="toc-number">2.1.</span> <span class="toc-text">模块化的初衷</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%BC%E5%87%BA-Export"><span class="toc-number">2.2.</span> <span class="toc-text">导出 Export</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5-Import"><span class="toc-number">2.3.</span> <span class="toc-text">导入 Import</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0-%E5%AF%BC%E5%85%A5-Import-%E5%92%8C-%E5%AF%BC%E5%87%BA-Export"><span class="toc-number">2.4.</span> <span class="toc-text">实现 导入 Import 和 导出 Export</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%B9%E9%87%8F-%E5%AF%BC%E5%85%A5-%E5%92%8C-%E5%AF%BC%E5%87%BA"><span class="toc-number">2.5.</span> <span class="toc-text">批量 导入 和 导出</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%87%8D%E5%91%BD%E5%90%8D%E5%AF%BC%E5%87%BA%E7%9A%84%E5%8F%98%E9%87%8F"><span class="toc-number">2.6.</span> <span class="toc-text">重命名导出的变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B4%E4%BD%93%E5%AF%BC%E5%85%A5"><span class="toc-number">2.7.</span> <span class="toc-text">整体导入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%BB%98%E8%AE%A4%E5%AF%BC%E5%87%BA"><span class="toc-number">2.8.</span> <span class="toc-text">默认导出</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">2.9.</span> <span class="toc-text">注意事项</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A3%B0%E6%98%8E%E7%9A%84%E5%8F%98%E9%87%8F-%E5%AF%B9%E5%A4%96%E9%83%BD%E6%98%AF%E5%8F%AA%E8%AF%BB%E7%9A%84"><span class="toc-number">2.9.1.</span> <span class="toc-text">声明的变量 , 对外都是只读的</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E4%B8%8D%E5%AD%98%E5%9C%A8%E7%9A%84%E5%8F%98%E9%87%8F%E4%BC%9A%E6%8A%A5%E9%94%99"><span class="toc-number">2.9.2.</span> <span class="toc-text">导入不存在的变量会报错</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/01/17/linux/linux-grub/" title="linux grub 恢复"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="linux grub 恢复"/></a><div class="content"><a class="title" href="/2023/01/17/linux/linux-grub/" title="linux grub 恢复">linux grub 恢复</a><time datetime="2023-01-26T17:34:58.993Z" title="更新于 2023-01-27 01:34:58">2023-01-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/08/ts/ts-dataType/" title="Ts 学习总结"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/ts/ts.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="Ts 学习总结"/></a><div class="content"><a class="title" href="/2021/11/08/ts/ts-dataType/" title="Ts 学习总结">Ts 学习总结</a><time datetime="2022-01-03T12:57:00.880Z" title="更新于 2022-01-03 20:57:00">2022-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/09/15/ts/ts-install-typescript/" title="TypeScript 环境安装"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/cover/tree.png" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="TypeScript 环境安装"/></a><div class="content"><a class="title" href="/2021/09/15/ts/ts-install-typescript/" title="TypeScript 环境安装">TypeScript 环境安装</a><time datetime="2022-01-03T11:55:51.110Z" title="更新于 2022-01-03 19:55:51">2022-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/29/js/js-common-require/" title="关于 commonjs &amp; requirejs"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="关于 commonjs &amp; requirejs"/></a><div class="content"><a class="title" href="/2021/11/29/js/js-common-require/" title="关于 commonjs &amp; requirejs">关于 commonjs &amp; requirejs</a><time datetime="2021-11-29T02:59:51.798Z" title="更新于 2021-11-29 10:59:51">2021-11-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/18/vue/vue-build-optimization/" title="Vue 打包优化"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/vue/vue.jpg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="Vue 打包优化"/></a><div class="content"><a class="title" href="/2021/11/18/vue/vue-build-optimization/" title="Vue 打包优化">Vue 打包优化</a><time datetime="2021-11-18T16:14:49.447Z" title="更新于 2021-11-19 00:14:49">2021-11-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By HHR</div><div class="footer_custom_text">In case I don’t see you ... Good afternoon,good evening,and good night.<p><a target="_blank" rel="noopener" href='https://beian.miit.gov.cn/' style='color:#00CCFF'>冀ICP备2020027302号-1</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-moon"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="https://js.exj-0224.cc/slot?7255774253627174051-11305"></script><script src="/js/onload.js"></script><script src="/js/artitalk.js"></script><script src="/js/formatted.js"></script><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script src="/js/jquery.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>